//
//  COCreateViewController.swift
//  CocoProject
//
//  Created by Ruthless on 2023/8/20.
//

import UIKit
import QMUIKit

class COCreateViewController: COBaseViewController {
    private var animationDelegate: COPresentAnimationDelegate = COPresentAnimationDelegate()
    
    var clickAction: ((Int)->Void)?
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nil, bundle: nil)
        self.modalPresentationStyle = .custom
        self.transitioningDelegate = animationDelegate
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override var navShow: Bool {
        false
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    override func configUI() {
        
        view.addSubview(mainView)
        mainView.addSubview(backImageView)
        mainView.addSubview(squareButton)
        mainView.addSubview(qaButton)
        mainView.addSubview(closeButton)
        mainView.addSubview(moneyButton)
        
        mainView.snp.makeConstraints { make in
//            make.left.right.bottom.equalToSuperview()
            make.edges.equalToSuperview()
            make.height.equalTo(220+bottomSafeHeight)
        }
        
        backImageView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(-1)
            make.bottom.top.equalToSuperview().offset(0)
        }
        
        squareButton.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(66)
            make.top.equalToSuperview().offset(31)
        }
        
        qaButton.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-66)
            make.top.equalToSuperview().offset(31)
        }
        
        closeButton.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-26-bottomSafeHeight)
            make.centerX.equalToSuperview()
        }
        
        moneyButton.snp.makeConstraints { make in
            make.centerX.equalTo(qaButton)
            make.top.equalTo(qaButton.snp.bottom).offset(5)
        }
    }

    private lazy var mainView: UIView = {
        let view = UIView()
        view.backgroundColor = .clear
        return view
    }()
    
    private lazy var backImageView: UIImageView = {
        let imgV = UIImageView(image: UIImage(named: "矩形"))
        return imgV
    }()
    
    
    private lazy var squareButton: QMUIButton = {
        let btn = QMUIButton()
        btn.setTitle("Square", for: .normal)
        btn.setImage(UIImage(named: "编组 9"), for: .normal)
        btn.imagePosition = .top
        btn.spacingBetweenImageAndTitle = 6
        
        btn.titleLabel?.font = .bold(18)
        btn.setTitleColor(.white, for: .normal)
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)

        return btn
    }()
    
    private lazy var qaButton: QMUIButton = {
        let btn = QMUIButton()
        btn.setTitle("Q&A area", for: .normal)
        btn.setImage(UIImage(named: "编组 10"), for: .normal)
        btn.imagePosition = .top
        btn.spacingBetweenImageAndTitle = 6
        btn.titleLabel?.font = .bold(18)
        btn.setTitleColor(.white, for: .normal)
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        return btn
    }()
    
    private lazy var moneyButton: QMUIButton = {
        let btn = QMUIButton()
        btn.setTitle("30", for: .normal)
        btn.setImage(UIImage(named: "金币"), for: .normal)
        btn.spacingBetweenImageAndTitle = 3
        btn.titleLabel?.font = .regular(12)
        btn.setTitleColor(.white, for: .normal)
        btn.isUserInteractionEnabled = false
        return btn
    }()
    
    private lazy var closeButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "编组 5"), for: .normal)
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)

        return btn
    }()
}

extension COCreateViewController {
    @objc
    private func clickButton(_ btn: UIButton) {
        if btn == squareButton {
            clickAction?(0)
        } else if btn == qaButton {
            clickAction?(1)
        } else {
            self.dismiss(animated: true)
        }
    }
}
